﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Drawing.Imaging;

namespace IEWrapper
{
    public partial class MainForm : Form
    {
        private string url;

        public string URL
        {
            get { return url; }
            set { url = value; }
        }

        public MainForm(string url)
        {
            this.URL = url; 

            InitializeComponent();
            this.Size = IEWrapper.Properties.Settings.Default.FormSize;
            this.Location = IEWrapper.Properties.Settings.Default.FormLocation;


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate(url);
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.Text = webBrowser1.DocumentTitle;
            string hostUrl = e.Url.Host;
            System.Drawing.Icon ico = GetFavIcon(hostUrl);
            this.Icon = ico;
            ico.Dispose();

            labelStatus.Text = webBrowser1.StatusText;
            labelStatus.Visible = false;

            this.webBrowser1.Document.MouseOver += new HtmlElementEventHandler(this.Browser_Mouse_Moved);

            

        }

        public System.Drawing.Icon GetFavIcon(string hostUrl)
        {
            WebRequest requestImg = WebRequest.Create("http://" + hostUrl + "/favicon.ico");
            //requestImg
            Stream myStream;
            Image img;
            WebResponse response = requestImg.GetResponse();
            
            if (response.ContentLength > 0)
                myStream = response.GetResponseStream();
            else
            {
                HtmlDocument doc = webBrowser1.Document;
                
                HtmlElementCollection collect = doc.GetElementsByTagName("link");
                string iconPath = "";
                foreach (HtmlElement element in collect)
                {
                    if (element.GetAttribute("rel") == "SHORTCUT ICON")
                        iconPath = element.GetAttribute("href");
                }

                //this.Text = doc.Title;
                requestImg = WebRequest.Create("http://" + hostUrl + iconPath);
                response = requestImg.GetResponse();
                if (response.ContentLength > 0)
                {
                    requestImg = WebRequest.Create(iconPath);
                    response = requestImg.GetResponse();
                }


                myStream = response.GetResponseStream();
            }

            img = Image.FromStream(myStream);
            Bitmap b = new Bitmap(img);
            System.IntPtr iconHandle = b.GetHicon();
            System.Drawing.Icon ico = Icon.FromHandle(iconHandle);
            b.Dispose();
            return ico;
        }

        private void Browser_Mouse_Moved(object sender, HtmlElementEventArgs e)
        {

            string element = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).GetAttribute("href");
            if (element != "")
            {
                labelStatus.Visible = true;
                int textWidth = this.CreateGraphics().MeasureString(element, this.Font).ToSize().Width;
                if (textWidth >= (this.Width - 5))
                {
                    labelStatus.Text = element.Substring(0, Convert.ToInt32(this.Width / 8)) + " ...";
                    labelStatus.Width = this.Width - 10;
                }
                else
                {
                    labelStatus.Text = element;
                    labelStatus.Width = textWidth;
                }
            }
            else
                labelStatus.Visible = false;

        }


        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            labelStatus.Visible = true;
            labelStatus.Text = webBrowser1.StatusText;
        }


        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Save();
            Application.Exit();
        }

        private void MainForm_LocationChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.FormLocation = this.Location;
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.FormSize = this.Size;
        }


 



    }
}
